﻿using System;
using System.Collections;
using UnityEngine;

namespace Scripts
{
    public class Tower : MonoBehaviour
    {
        public Transform head;
        public Transform firePoint;
        State state = State.idle;//防御塔状态
        public float atkRange = 7f;//攻击范围
        private Transform targetMonster;//目标敌人
        private float nextTime;//下次发射子弹的时间
        public GameObject bulletPrefab;//子弹预制体
        [SerializeField]
        private float shootInterval=0.5f;//发射子弹的间隔时间

        public float sqrAtkRange => atkRange * atkRange;//攻击范围的平方
        private void Update()
        {
            switch (state)
            {
                case State.idle:
                    Idle();
                    break;
                case State.rotate:
                    Rotate();
                    break;
                case State.atk:
                    Atk();
                    break;
                default:
                    break;
            }
        }

        //闲置状态-找攻击目标   找到目标->旋转状态
        private void Idle()
        {
            var list = MonsterCreater.monsterList;
            for (int i = 0; i < list.Count; i++)
            {
                if (isMonsterInRange(list[i]))
                {
                    targetMonster = list[i];
                    state = State.rotate;//修改状态
                    return;
                }
            }
        }
        // /旋转状态-转向目标 旋转完成->攻击状态 目标走处范围->闲置状态
        private void Rotate()
        {
            if (!targetMonster ||!isMonsterInRange(targetMonster))
            {
                state = State.idle;
                return;
            }
            //头的目标旋转
            var targetRotation=  Quaternion.LookRotation(targetMonster.position - head.position);
            //当前旋转插值到目标旋转
            head.rotation = Quaternion.RotateTowards(head.rotation,targetRotation,Time.deltaTime*360);

            if (Quaternion.Angle(targetRotation, head.rotation)<10f)
            {
                nextTime = Time.time;//设置下次发射子弹的时间
                state = State.atk;//切换到攻击状态
            }  
            
        }
        //攻击状态-发射子弹攻击敌人  敌人死亡->闲置状态   目标走处范围->闲置状态
        private void Atk()
        {
            //如果敌人死亡或者敌人走出范围,回到闲置状态
            if (!targetMonster || !isMonsterInRange(targetMonster))
            {
                state = State.idle;
                return;
            }   


            Debug.Log("攻击敌人");
            head.LookAt(targetMonster);
            //间隔时间发射子弹
            if (Time.time>nextTime)
            {
                //生成子弹
               var buleltGo=  Instantiate(bulletPrefab, firePoint.position, firePoint.rotation) ;
                //给子弹设置目标敌人
                buleltGo.GetComponent<Bullet>().SetTargetMonster(targetMonster);

                nextTime +=shootInterval;
            }
        }

        bool isMonsterInRange(Transform monster) 
        {
            return (monster.position - transform.position).sqrMagnitude < sqrAtkRange;
        }

        //画出攻击范围,方便调试
        private void OnDrawGizmos()
        {
#if UNITY_EDITOR //只有在编辑器状态下才会编辑#if中的代码
            if (Array.IndexOf(UnityEditor.Selection.gameObjects ,this.gameObject)!=-1)
            {
                Gizmos.color = Color.green;
                Gizmos.DrawWireSphere(transform.position, atkRange);
            }
#endif
        }
    }

    enum State
    {
        idle,//闲置状态
        rotate,//旋转状态-
        atk//攻击状态
    }
}

//状态模式,当前在什么状态就执行什么逻辑